import json


class RecordHistory():
    """用户记录操作类"""

    def __init__(self):
        """初始化时读取历史记录"""
        self.filename = 'record/score.json'
        self.record_data = {}
        try:
            with open(self.filename, 'r+') as file_object:
                if file_object:
                    self.record_data = json.loads(file_object.read())
        except IOError:
            pass

    def get_high_score(self):
        """获取最高分数"""
        if 'high_score' in self.record_data.keys():
            return self.record_data['high_score']
        else:
            return 0

    def store_high_score(self, high_score):
        """存储最高分"""
        try:
            self.record_data['high_score'] = high_score
            with open(self.filename, 'w') as file_object:
                file_object.write(json.dumps(self.record_data))
        except IOError:
            pass
